package com.ruoyi.project.business.domain.rules.month;

import com.ruoyi.project.business.domain.entity.Emp;
import com.ruoyi.project.business.domain.entity.ScheduleDefineMirror;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.rules.Rule;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 想上的、不想上的班次
 *
 */
@Data
public class SchedefineWish extends Rule {
    private List<ScheduleDefineMirror> scheduleDefines;
    private List<ScheduleDefineMirror> wantList;
    private List<ScheduleDefineMirror> notWantList;


    @Override
    public JudgeResult judge(Solution solution, Emp emp) {
        ScheduleDefineMirror wanted = findWantedScheduleDefine();

        if(wanted != null) {
            JudgeResult accept = accept();
            accept.setScheduleDefine(wanted);
            return accept;
        } else {
            if (getBreakIfReject()) {
                return stopTask();
            } else {
                return acceptAndReturn();
            }
        }
//        throw new RuleException(this.getClass().getName(), -1);
    }

    private ScheduleDefineMirror findWantedScheduleDefine() {
        //
        if(CollectionUtils.isNotEmpty(wantList)) {
            for (ScheduleDefineMirror want : wantList) {
                for (ScheduleDefineMirror scheduleDefine : scheduleDefines) {
                    if(scheduleDefine.getId().equals(want.getId())) {
                        return scheduleDefine;
                    }
                }
            }
        }
        return scheduleDefines.get(0);
    }

    @Override
    public void setConflicts() {
        setType(Rule.RULE_TYPE_USER);
        this.conflicts = new ArrayList<>();
    }
}
